/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.confiteriofuentes.modelo;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 *
 * @author Sala4
 */
public class Model {

    private Connection con;
    private String jdbcDriver;
    private String jdbcUser;
    private String jdbcPassword;
    private String jdbcDataBaseURL;
    private static Model model;

    private Properties properties;

    private Model() throws SQLException {
        properties = new Properties();
        try {
            properties.load(new FileInputStream(System.getProperty("user.dir") + System.getProperty("file.separator") + "confConexion.properties"));


            jdbcDataBaseURL = properties.getProperty("jdbcDataBaseURL");
            jdbcDriver = properties.getProperty("jdbcDriver");
            jdbcUser = properties.getProperty("jdbcUser");
            jdbcPassword = properties.getProperty("jdbcPassword");

        } catch (Exception ex) {
            ex.printStackTrace();
            throw new SQLException("Error, imposible conectarse a la base de datos, por favor verifique los parámetros de conexión....!");
        }
    }

    private void conectar() throws SQLException {
        if (con == null || con.isClosed()) {
            if (jdbcDriver == null) {
                throw new SQLException("Error, no hay Driver de Conexión...!");
            }
            if (jdbcDataBaseURL == null) {
                throw new SQLException("Error, no ha especificado la URL del Servidor de BD...!");
            }


            try {
                Class.forName(jdbcDriver);
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
                throw new SQLException("Error, no fue posible cargar el Driver de conexión...!");
            }

            con = DriverManager.getConnection(jdbcDataBaseURL, jdbcUser, jdbcPassword);
            System.out.println("Conectado...");
        }
    }

    public static Model getInstance() throws SQLException {
        if (model == null) {
            model = new Model();
        }

        model.conectar();
        return model;
    }

    public boolean isConnect() throws SQLException {
        return con != null && !con.isClosed();
    }

    public Connection getCon() {
        return con;
    }

      public static ClienteDAO getClienteDAO() throws SQLException {
        ClienteDAO dao = new ClienteDAO();

        return dao;
    }

 
}
